import {Component, OnInit} from '@angular/core';
import {Student} from '../student';
import {StudentService} from '../student.service';

@Component({
  selector: 'app-student-detail',
  templateUrl: './student-detail.component.html',
  styleUrls: ['./student-detail.component.css']
})
export class StudentDetailComponent implements OnInit {
  students: Student[];
  student: Student = {
    id: -1,
    name: '请输入姓名',
    gender: '请输入性别',
    degree: '请输入学历',
    born: '请输入生日',
    tel: '请输入手机号',
    addr: '请输入地址'
  };

  constructor(private studentService: StudentService) {
  }

  ngOnInit() {
  }

  onList() {
    this.studentService.list().subscribe(stu => this.students = stu);
  }

  onSave() {
    this.studentService.save(this.student);
    this.onList();
  }

  onDelete(id: number) {
    this.studentService.delete(id).subscribe();
    this.onList();
  }

  onUpdate() {
    this.studentService.update(this.student);
    this.onList();
  }

  onGet(id: number) {
    this.studentService.get(id).subscribe(stu => this.student = stu);
  }

  onOperate() {
    if (this.student.id > 0) {
      console.log(this.student.id);
      this.onUpdate();
    } else {
      this.onSave();
    }
  }
}
